﻿import java.util.Hashtable;
import javax.swing.*;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
public class test10 extends JPanel 
{
	static final int WIDTH=300;
	static final int HEIGHT=800;
	JTree tree;
	JLabel label;
test10()
	{
		JFrame frame=new JFrame();
		frame.setTitle("树组件测试窗口");
		frame.setSize(WIDTH,HEIGHT);
		frame.setVisible(true);
		frame.pack();
		
		 DefaultMutableTreeNode root=new DefaultMutableTreeNode("家庭用品");//使用DefaultMutableTreeNode的构造器创建根节点
         ///使用DefaultMutableTreeNode的构造器创建四个枝节点	
		 DefaultMutableTreeNode node1=new DefaultMutableTreeNode("家用电器");
		 DefaultMutableTreeNode node2=new DefaultMutableTreeNode("厨具");
		 DefaultMutableTreeNode node3=new DefaultMutableTreeNode("家具");
		 DefaultMutableTreeNode node4=new DefaultMutableTreeNode("书");

		 root.add(node1);
		 root.add(node2);
		 root.add(node3);
		 root.add(node4);
		 
		 DefaultMutableTreeNode leafnode=new DefaultMutableTreeNode("电视机");
		 node1.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("洗衣机");
		 node1.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("影碟机");
		 node1.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("空调");
		 node1.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("冰箱");
		 node1.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("微波炉");
		 node1.add(leafnode);
		    
		 leafnode=new DefaultMutableTreeNode("锅");
		 node2.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("汤勺");
		 node2.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("搅拌机");
		 node2.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("锅铲");
		 node2.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("菜刀");
		 node2.add(leafnode);
		    
		 leafnode=new DefaultMutableTreeNode("大衣柜");
		 node3.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("饭桌");
		 node3.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("床");
		 node3.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("书桌");
		 node3.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("书柜");
		 node3.add(leafnode);
		 
		 leafnode=new DefaultMutableTreeNode("电脑书");
		 node4.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("小说");
		 node4.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("课本");
		 node4.add(leafnode);
		 leafnode=new DefaultMutableTreeNode("字典");
		 node4.add(leafnode);
		 
		 JTree tree=new JTree(root);
		 JScrollPane scrollPane=new JScrollPane();
		 scrollPane.setViewportView(tree);

		 add(scrollPane);
		 
		 label=new JLabel("你要选择吗？");
		 add(label);
	     frame.setContentPane(this);	     tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
	     tree.addTreeSelectionListener(new TreeSelectionListener() 
	     {
	           public void valueChanged(TreeSelectionEvent e) 
	     {
//	     /将所选的节点模型化
	             DefaultMutableTreeNode node = (DefaultMutableTreeNode)(DefaultMutableTreeNode) e
	             .getPath().getLastPathComponent();
	             label.setText("你所选择的是： " + node);
	           }
	         });
	}
	public static void main(String[] args)
	{
		new test10();
	}	
}
